package com.supermap.system.service;

import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.DataDubboConcumerService;
import com.supermap.util.SQLFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.supermap.tool.DaoTool;
import com.supermap.tool.MysqlEntity;
import com.supermap.tool.SqlCx;
import com.supermap.tool.TableTool;
import com.supermap.tool.TableVoo;

import javax.annotation.Resource;


@Service
public class RoleService {

    @Autowired
    TableTool tableTool;
    @Resource
    DataDubboConcumerService dataDubboConcumerService;

    /**
     * @param @return 设定文件
     * @return Object    返回类型
     * @throws
     * @Title: getTreeList
     * @Description: 获取角色列表
     */
    public TableVoo getRoleList(TableVoo table) {

        StringBuilder sql = new StringBuilder();
        sql.append("select * from t_role ");

        String con = "select count(1) count from t_role ";

        return tableTool.getTable(table, sql.toString(), con, "t_role");
    }

    /**
     * @param name
     * @param roleId
     * @param code
     * @param cdList
     * @return void    返回类型
     * @throws
     * @Title: updateRole
     * @Description: 修改角色权限
     */
    public int updateRole(String name, String roleId, String code, String[] cdList) {

        MysqlEntity st = new MysqlEntity();
        st.setTableName("t_role");
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE t_role SET ");
        sql.append("name='" + name + "',");
        sql.append("code='" + code + "' ");
        sql.append("WHERE id='" + roleId + "'");
        st.setSql(sql.toString());
        int updateStatus = dataDubboConcumerService.getDataWithUpdate(sql.toString()).getInteger("code");

        if (updateStatus != 200) {
            return -1;
        }

        st.setTableName("sys_role_menu");
        String deleteSql = "DELETE FROM sys_role_menu WHERE role_id = '" + roleId + "'";
        st.setSql(deleteSql);
        int deleteStatus = dataDubboConcumerService.getDataWithDelete(deleteSql).getInteger("code");
        if (deleteStatus != 200) {
            return -1;
        }

        sql.delete(0, sql.length());
        sql.append("INSERT INTO sys_role_menu (menu_id,role_id) values ");
        for (String cd : cdList) {
            sql.append("('" + cd + "','" + roleId + "'),");
        }
        sql.deleteCharAt(sql.length() - 1);
        st.setSql(sql.toString());
        int insertStatus = dataDubboConcumerService.getDataWithInsert(sql.toString()).getInteger("code");

        if (insertStatus == 200) {
            return 0;
        }
        return -1;
    }

    /**
     * @param @param  id
     * @param @return 设定文件
     * @return Object    返回类型
     * @throws
     * @Title: getUserRoleList
     * @Description: 通过角色获取 菜单ID
     */
    public Object getUserRoleList(String id) {

        StringBuilder sql = new StringBuilder();
        sql.append("select menu_id as id from sys_role_menu where role_id = ");
        sql.append(id);
        MysqlEntity st = new MysqlEntity();
        st.setTableName("sys_menu");
        st.setSql(sql.toString());
        return dataDubboConcumerService.getDataWithSelect(sql.toString());
    }

    /**
     * @param @return 设定文件
     * @return Object    返回类型
     * @throws
     * @Title: getRoleList
     * @Description: 获取 有效的角色 列表
     */
    public Object getRoleList() {
        StringBuilder sql = new StringBuilder();
        MysqlEntity st = new MysqlEntity();
        st.setTableName("t_role");
        JSONObject cv = new JSONObject();
        cv.put("del_flg", "0");
        st.setConditionValue(cv);
        st.setSql(sql.toString());
        String fastQuerySql = SQLFactory.getFastQuerySql(st);
        return dataDubboConcumerService.getDataWithSelect(fastQuerySql);
    }
}
